package com.mall.web.controller.product;

import com.mall.common.config.Global;
import com.mall.common.utils.file.FileUploadUtils;
import com.mall.product.domain.biz.ProProductBiz;
import com.mall.product.domain.biz.ProProductImageBiz;
import com.mall.product.domain.entity.ProBrand;
import com.mall.product.service.ProProductService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.mall.common.annotation.Log;
import com.mall.common.enums.BusinessType;
import com.mall.product.domain.entity.ProProductImage;
import com.mall.product.service.ProProductImageService;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品图片Controller
 *
 * @author zhonglin
 * @date 2020-04-29
 */
@Controller
@RequestMapping("/product/image")
public class ProProductImageController
    extends BaseController {

  private static final Logger log    = LoggerFactory.getLogger(ProProductImageController.class);
  private              String prefix = "product/image";

  @Autowired
  private ProProductImageService proProductImageService;
  @Autowired
  private ProProductService      proProductService;

  @RequiresPermissions("product:image:view")
  @GetMapping()
  public String image() {

    return prefix + "/image";
  }

  /**
   * 查询商品图片列表
   */
  @RequiresPermissions("product:image:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProProductImage proProductImage) {

    startPage();
    List<ProProductImageBiz> list = proProductImageService.selectProProductImageList(proProductImage);
    return getDataTable(list);
  }

  /**
   * 导出商品图片列表
   */
  @RequiresPermissions("product:image:export")
  @Log(title = "商品图片", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProProductImage proProductImage) {

    List<ProProductImageBiz>      list = proProductImageService.selectProProductImageList(proProductImage);
    ExcelUtil<ProProductImageBiz> util = new ExcelUtil<ProProductImageBiz>(ProProductImageBiz.class);
    return util.exportExcel(list, "image");
  }

  /**
   * 新增商品图片
   */
  @GetMapping("/add")
  public String add() {

    return prefix + "/add";
  }

  /**
   * 新增保存商品图片
   */
  @RequiresPermissions("product:image:add")
  @Log(title = "商品图片", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProProductImage proProductImage) {

    return toAjax(proProductImageService.insertProProductImage(proProductImage));
  }

  /**
   * 修改商品图片
   */
  @GetMapping("/edit/{productImageId}")
  public String edit(@PathVariable("productImageId") Long productImageId,
                     ModelMap mmap) {

    ProProductImageBiz proProductImage = proProductImageService.selectProProductImageById(productImageId);
    mmap.put("proProductImage", proProductImage);
    return prefix + "/edit";
  }

  /**
   * 修改保存商品图片
   */
  @RequiresPermissions("product:image:edit")
  @Log(title = "商品图片", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProProductImage proProductImage) {

    return toAjax(proProductImageService.updateProProductImage(proProductImage));
  }

  /**
   * 删除商品图片
   */
  @RequiresPermissions("product:image:remove")
  @Log(title = "商品图片", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proProductImageService.deleteProProductImageByIds(ids));
  }

  @GetMapping("/upload/{productId}")
  public String upload(@PathVariable("productId") Long productId,
                       ModelMap mmap) {

    ProProductBiz proProduct = proProductService.selectProProductById(productId);
    mmap.put("product", proProduct);
    return prefix + "/upload";
  }

  @PostMapping("/upload/{productId}")
  public AjaxResult uploadSave(@PathVariable("productId") Long productId,
                               @RequestParam(value = "file_data") MultipartFile file) {

    ProProductImage proProductImage = new ProProductImage();
    proProductImage.setProductId(productId);

    try {
      if (file != null && !file.isEmpty()) {
        String url = FileUploadUtils.upload(Global.getAvatarPath(), file);
        proProductImage.setImageUrl(url);
      }
    } catch (Exception e) {
      log.error("上传图片失败！", e);
      return error(e.getMessage());
    }
    return toAjax(proProductImageService.insertProProductImage(proProductImage));
  }

}
